#!/bin/sh

################################################
# Prog 	: getns.sh
# Desc 	: get the file new slave zone file, and
#	  restart named if needed.
# Info	: 
#   -you may run it one manually to create files
#   -you may add this script to your cron
#
################################################
# Comments : johan Lenglet ( webcp@100p100.net )
#       for 100% Net (http://www.100p100.net)
################################################

################################################
# Variables
################################################

#***********************************************
# Servers
#***********************************************

# Note :
#  To add/delete servers just copy/delete 
#  the struct with DISTFILE[0], SERVER[0]
#  and SSL[0] and change 0 by the server 
#  number

# How many servers do you need to check ?
NUMSERVER=3

#------------------
# Server 0
#------------------

# what file do need I to get ?
DISTFILE[0]='ns2.named'

# where to get the file ?
SERVER[0]='domain0.com:81'

# use https ( 0/1 )
SSL[0]=1

#------------------
# Server 1
#------------------

# what file do need I to get ?
DISTFILE[1]='ns2.named'

# where to get the file ?
SERVER[1]='domain1.com:81'

# use https ( 0/1 )
SSL[1]=1

#------------------
# Server 2
#------------------

# what file do need I to get ?
DISTFILE[2]='ns2.named'

# where to get the file ?
SERVER[2]='domain2.com:81'

# use https ( 0/1 )
SSL[2]=1

#***********************************************
# Local files
#***********************************************

# What is the dns include directory and file ?
LOCALFILE='/var/named/ns2.named'

# Where do I have to save the current file 
# before to merge it with LOCALFILE ?
TMPFILE='/tmp/tmp.named'

# Where do I have to save the old file ?
OLDFILE='/tmp/ns2.named'

# HTTP error code to check requests
ERRORFILE='/tmp/error.named'

# Error message header
ERRORMSG="/tmp/errormsg.named"

# Dns admin EMAIL (for errors)
EMAIL='root'

#***********************************************
# Others
#***********************************************

# DEBUG (0/1)
DEBUG=1

################################################
# Program
################################################

# test LOCALFILE
if [ $DEBUG = 1 ]; then
	echo " -> test $LOCALFILE ..." 
fi
if [ ! -f "$LOCALFILE" ] 
then
	echo "File $LOCALFILE not found!"
	echo "Do you want to create the file and continue ? (o/n)"
	read ANSWER
	case "$ANSWER" in
		"O" | "o" )
		# Accept upper or lowercase input.
			if [ $DEBUG = 1 ]
			then
				echo " -> create $LOCALFILE ..." 
			fi
			touch $LOCALFILE	
		;;
		"N" | "n" )
			echo "bye ..."
			exit
		;;
		* )
		# Default option.
			echo "Problem ... bye ..."
			exit
		;;
	esac
fi

# copy old file
if [ $DEBUG = 1 ]; then
	echo " -> save $LOCALFILE to $OLDFILE..." 
fi
cp $LOCALFILE $OLDFILE

# clean file
if [ $DEBUG = 1 ]; then
        echo " -> new $LOCALFILE ..."
fi
rm -f $LOCALFILE
touch $LOCALFILE

# Set variables
ERROR=0
I=0;
echo "Unable to get : " > $ERRORMSG

# Check every defined servers
while [ "$I" -lt "$NUMSERVER" ]
do
	# get new file
	if [ "${SSL[$I]}" == 1 ]
	then
		if [ $DEBUG = 1 ]; then
			echo " -> get https://${SERVER[$I]}/${DISTFILE[$I]}..." 
		fi
		lynx -dump https://${SERVER[$I]}/${DISTFILE[$I]} -error_file=$ERRORFILE > $TMPFILE
	else
		if [ $DEBUG = 1 ]; then
			echo " -> get http://${SERVER[$I]}/${DISTFILE[$I]}..." 
		fi
		lynx -dump http://${SERVER[$I]}/${DISTFILE[$I]} -error_file=$ERRORFILE > $TMPFILE
	fi

	# check error_code
	if [ -f "$ERRORFILE" ]
	then
		err=`cat $ERRORFILE | tail -1 | awk '/ / {printf $2}'`
		if [ "$err" != 200 ]
		then
			if [ $DEBUG = 1 ]; then
				echo " -> http(s) error : $err..."
			fi
			ERROR=1
			echo "${SERVER[$I]}/${DISTFILE[$I]} : error $err" >> $ERRORMSG
		else
			# add to new named file
			cat $TMPFILE >> $LOCALFILE
		fi
	else
		if [ $DEBUG = 1 ]; then
                	echo " -> Unknown host..."
                fi
                ERROR=1
                echo "${SERVER[$I]} : Unknown host" >> $ERRORMSG

	fi

	# clean before to check the next server
	rm -f $TMPFILE $ERRORFILE

	# I++
	I=`expr $I + 1`
done

# check validity
if [ $ERROR -eq 1 ]
then
	if [ $DEBUG = 1 ]; then
		echo " -> mail $EMAIL..." 
	fi
	cat $ERRORMSG | mail -s "update 2nd DNS ERROR" $EMAIL
fi

# Restart named if needed
# diff 2 files
dif=`diff $LOCALFILE $OLDFILE`
if [ $DEBUG = 1 ]; 
then
	echo " -> diff between $LOCALFILE $OLDFILE $dif ..."
fi
if [ -n "$dif" ]
then
	if [ $DEBUG = 1 ]; then
		echo " -> reboot named..." 
	fi
	/etc/init.d/named restart 2>/dev/null >/dev/null
fi

# clean files
if [ $DEBUG = 1 ]; then
	echo " -> delete files : $ERRORFILE $TMPFILE $OLDFILE $ERRORMSG" 
fi
rm -f $ERRORFILE $TMPFILE $OLDFILE $ERRORMSG

if [ $DEBUG = 1 ]; then
	echo "done!" 
fi
################################################
# End
################################################
